package com.baibei.ebec.home.shipment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.ebec.home.common.ShipmentInfoFragment;
import com.baibei.ebec.home.index.HomeIndexContract;
import com.baibei.model.Area;
import com.baibei.model.BannerInfo;
import com.baibei.model.LastPriceInfo;
import com.baibei.model.ProductInfo;
import com.baibei.model.QuotationInfo;
import com.baibei.model.RankingInfo;
import com.baibei.model.ShipmentInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.LazyLoadFragment;
import com.baibei.ui.AppUI;
import com.baibei.widget.CommonRefreshLayout;
import com.blankj.utilcode.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 订购碎片
 * shenminjie 2017-8-7
 */
public class ShipmentFragment extends LazyLoadFragment implements HomeIndexContract.View, ShipmentAdapter.OnAdapterListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refresh_layout)
    CommonRefreshLayout mRefreshLayout;
    ShipmentAdapter mAdapter;
    private String mType;

    private HomeIndexContract.Presenter mPresenter;
    private List<ShipmentInfo> mDatas;

    public ShipmentFragment() {
    }

    /**
     * 入参类型
     */
    public static ShipmentFragment newInstance(String param1) {
        return newInstance(param1, true);
    }

    /**
     * 入参类型
     */
    public static ShipmentFragment newInstance(String param1, boolean isRefresh) {
        ShipmentFragment fragment = new ShipmentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putBoolean(ARG_PARAM2, isRefresh);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mType = getArguments().getString(ARG_PARAM1);
        }
        LogUtils.e("fragment:" + this);
        mPresenter = inject(HomeIndexContract.Presenter.class);
        mDatas = new ArrayList<>();
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new ShipmentAdapter(mDatas, this);
        mRecyclerView.setAdapter(mAdapter);
        mRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                mPresenter.start();
            }
        });
    }

    /**
     * 懒加载
     */
    public void lazyLoad() {
        mRefreshLayout.autoRefresh();
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_shipment;
    }


    @Override
    public void onLoadBannerData(List<BannerInfo> data) {
//        LogUtils.e("data:" + data);
    }

    @Override
    public void onLoadClosePrice(List<LastPriceInfo> data) {

    }

    @Override
    public ProductInfo getProduct() {
        return null;
    }

    @Override
    public void onEmptyBanner() {

    }

    @Override
    public void onLoadComplete() {
        closeRefresh();
    }

    @Override
    public void onLoadCloseprice(List<RankingInfo> info) {

    }

    @Override
    public void refreshCompleted() {

    }

    @Override
    public Area getArea() {
        return Area.valueOf(mType);
    }

    @Override
    public void onLoadBannerFailed(String s) {

    }

    @Override
    public void onLoadServiceQQSuccess(String qq) {

    }

    @Override
    public void onLoadServiceQQFailed(String msg) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showLoading() {

    }


    @Override
    public void onNoMoreData() {
        closeRefresh();
    }

    @Override
    public void onEmptyData(String s) {
        closeRefresh();
    }

    @Override
    public void onLoadData(List<ShipmentInfo> list) {
        closeRefresh();
        mDatas.clear();
        mDatas.addAll(list);
        mAdapter.updateInfoPosition();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoginExpired() {

    }

    @Override
    public void onShipmentInfoClickListener(ShipmentInfo info) {
        ShipmentInfoFragment.show(getChildFragmentManager(), info);
    }

    @Override
    public void onProctorInfoClickListener(ShipmentInfo shipmentInfo, ProductInfo productInfo) {
        if (shipmentInfo == null) {
            AppUI.failed(getContext(), "船信息不存在");
            return;
        }
        if (productInfo == null) {
            AppUI.failed(getContext(), "产品信息不存在");
            return;
        }
        AppRouter.routeToTrade(getContext(), shipmentInfo.getEstimateTime(), productInfo, shipmentInfo.getSaleTips());
    }

    /**
     * 关闭下拉刷新
     */
    private void closeRefresh() {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.refreshComplete();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        mPresenter.destroy();
        super.onDestroy();
    }
}
